package cn.ultrarisk.scadmin.manager.biz.user.service;

import cn.ultrarisk.mybatis.extension.query.Updater;
import cn.ultrarisk.scadmin.manager.biz.common.context.OperationType;
import cn.ultrarisk.scadmin.manager.biz.common.service.OperationLogService;
import cn.ultrarisk.scadmin.manager.biz.user.dao.AdminDAO;
import cn.ultrarisk.scadmin.manager.biz.user.dao.RoleDAO;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Admin;
import cn.ultrarisk.scadmin.manager.biz.user.valid.AdminValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 用户数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class AdminService {
    @Autowired
    private AdminDAO adminDAO;
    @Autowired
    private RoleDAO roleDAO;
    @Autowired
    private AdminValidator validator;
    @Autowired
    private OperationLogService operationLogService;

    public Admin create(String account, String password, String name, String email) {
        validator.validAccount(account);

        Admin admin = new Admin();
        admin.setAccount(account);
        admin.setPassword(password);
        admin.setName(name);
        admin.setEmail(email);
        admin.setDisabled(false);
        admin.setCreateTime(System.currentTimeMillis());
        adminDAO.create(admin);
        operationLogService.createAdminLog(OperationType.ADMIN_CREATE, admin.getId());
        return admin;
    }

    public void saveRole(Integer id, Map<Integer, Boolean> roles, Map<Integer, Map<Integer, Boolean>> collectionRoles) {
        roleDAO.deleteRoleByAdmin(id);
        roleDAO.createRoleByAdmin(id, roles, collectionRoles);
        operationLogService.createAdminLog(OperationType.ADMIN_ALLOCATE, id);
    }

    public void delete(Integer id) {
        operationLogService.createAdminLog(OperationType.ADMIN_DELETE, id);
        adminDAO.delete(id);
    }

    public void delete(List<Integer> ids) {
        operationLogService.createAdminLog(OperationType.ADMIN_DELETE, ids);
        adminDAO.delete(ids);
    }

    public void disable(Integer id) {
        adminDAO.update(id, Updater.update(Admin.DISABLED, true));
        operationLogService.createAdminLog(OperationType.ADMIN_DISABLE, id);
    }

    public void disable(List<Integer> ids) {
        adminDAO.update(ids, Updater.update(Admin.DISABLED, true));
        operationLogService.createAdminLog(OperationType.ADMIN_DISABLE, ids);
    }

    public void enable(Integer id) {
        adminDAO.update(id, Updater.update(Admin.DISABLED, false));
        operationLogService.createAdminLog(OperationType.ADMIN_ENABLE, id);
    }

    public void enable(List<Integer> ids) {
        adminDAO.update(ids, Updater.update(Admin.DISABLED, false));
        operationLogService.createAdminLog(OperationType.ADMIN_ENABLE, ids);
    }
}